package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.Stock;
import cn.stylefeng.guns.sys.modular.tao.mapper.StockMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.StockParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.StockResult;
import  cn.stylefeng.guns.sys.modular.tao.service.StockService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 库存 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-09-11
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {
    @Autowired
    StockMapper stockMapper;
    @Override
    public void add(StockParam param){
        Stock entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StockParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StockParam param){
        Stock oldEntity = getOldEntity(param);
        Stock newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StockResult findBySpec(StockParam param){
        return null;
    }

    @Override
    public List<StockResult> findListBySpec(StockParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StockParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageLists(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public StockParam getByNumId(String numid) {
        return stockMapper.getByNumId(numid);
    }

    @Override
    public Integer updateByNumId(StockParam stockParam) {
        return stockMapper.updateByNumId(stockParam);
    }

    @Override
    public StockParam getByNumIdAndSku(String numid, String s) {
        return stockMapper.getByNumIdAndSku(numid,s);
    }

    @Override
    public StockResult getGoodsSpecsByGoodsId(String id) {
        return stockMapper.getGoodsSpecsByGoodsId(id);
    }

    private Serializable getKey(StockParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Stock getOldEntity(StockParam param) {
        return this.getById(getKey(param));
    }

    private Stock getEntity(StockParam param) {
        Stock entity = new Stock();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
